.SILENT:

PYTHON       = @PYTHON@
VPATH        = @srcdir@
srcdir       = @srcdir@
top_srcdir   = @top_srcdir@
top_builddir = @top_builddir@

TESTS_DIR = $(srcdir)/tests
RESULTS_DIR = results
PORTS_DIR = $(srcdir)/ports
BUILD_DIR = gen
ALL_PORTS = mcs51 mcs51-large mcs51-stack-auto ds390 z80 z180 r2k sm83 tlcs90 hc08 s08 stm8 f8 pdk13 pdk14 pdk15 mos6502

ALL_TESTS = $(shell find $(TESTS_DIR) -name "*.c")

PORT_RESULTS_DIR = $(RESULTS_DIR)/$(PORT)
PORT_RESULTS = $(ALL_TESTS:$(TESTS_DIR)/%.c=$(PORT_RESULTS_DIR)/%.out)
PORT_BUILD_DIR = $(BUILD_DIR)/$(PORT)

all: test-ports

test-ports:
	for i in $(ALL_PORTS); do $(MAKE) test-port PORT=$$i; done

test-mcs51:
	$(MAKE) test-port PORT=mcs51

test-port: port-results

clean: clean-gen
	rm -rf $(RESULTS_DIR) *.pyc
	rm -rf $(BUILD_DIR)

distclean: clean
	rm -r Makefile

clean-gen:	
	for i in $(ALL_PORTS); do $(MAKE) clean-port PORT=$$i; done

clean-port:
	rm -rf $(PORT_BUILD_DIR)

$(PORT_RESULTS_DIR)/%.out: $(TESTS_DIR)/%.c port-dirs
	$(PYTHON) $(srcdir)/valdiag.py $(PORT) $< $(PORT_BUILD_DIR)/$* $(srcdir)/../../device/include > $@
	grep FAIL $@ ; true
	cat $@ | $(PYTHON) $(srcdir)/../regression/compact-results.py $*.c

port-results: port-dirs $(PORT_RESULTS)
	cat $(PORT_RESULTS) | $(PYTHON) $(srcdir)/../regression/collate-results.py $(PORT)

port-dirs:
	mkdir -p $(PORT_RESULTS_DIR)
	mkdir -p $(PORT_BUILD_DIR)

Makefile: $(srcdir)/Makefile.in
	 cd $(top_builddir); ./config.status support/valdiag/Makefile
